package Chapter22.CollectionsMethod;

import java.util.*;

public class SetListPerformanceTest {
    public static void main(String[] args) {
        // Create a hash set, and test its performance
        Collection<Integer> set1 = new HashSet<>();
        System.out.println("Time for hash set is: " + getTestTime(set1, 500000) + " milliseconds");

        // Create a linked hash set, and test its performance
        Collection<Integer> set2 = new LinkedHashSet<>();
        System.out.println("Time for linked hash set is: " + getTestTime(set2, 500000) + " milliseconds");

        // Create a linked tree set, and test its performance
        Collection<Integer> set3 = new TreeSet<>();
        System.out.println("Time for tree set is: " + getTestTime(set3, 500000) + " milliseconds");

        // Create a linked tree set, and test its performance
        Collection<Integer> list1 = new ArrayList<>();
        System.out.println("Time for array list is: " + getTestTime(list1, 500000) + " milliseconds");

        // Create a linked tree set, and test its performance
        Collection<Integer> list2 = new LinkedList<>();
        System.out.println("Time for linked list is: " + getTestTime(list2, 500000) + " milliseconds");
    }

    public static long getTestTime(Collection<Integer> c, int size) {
        long startTime = System.currentTimeMillis();

        // Add numbers 0, 1, 2, ..., size-1 to the Array list
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            list.add(i);
        }
        // shuffle the array list
        Collections.shuffle(list);

        // Add the element from the container
        for (int element : list) {
            c.add(element);
        }
        // shuffle the array list
        Collections.shuffle(list);

        // Remove the element from the container
        for (int element : list) {
            c.remove(element);
        }

        long endTime = System.currentTimeMillis();

        // Return the execution time
        return endTime - startTime;
    }
}
